﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECP.Utilities;
using ECP.Model;
using ECP.Service.Utilities;
using ECP.Utilities.Controls;

namespace ECP.Client.Module
{
    public partial class ModuleEditFrm : FormBase
    {

        private DataTable DTModule;
        private DataTable DTParent;

        public string EntityID;
        public ModuleEditFrm()
        {
            this.DTModule = new DataTable(BaseModuleTable.TableName);
            this.EntityID = string.Empty;
            this.InitializeComponent();
        }

        public ModuleEditFrm(string id)
            : this()
        {
            this.EntityID = id;
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnPothook_Click(object sender, EventArgs e)
        {
            this.Pothook();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.CheckInput())
            {
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    if (this.SaveEntity())
                    {
                        base.Close();
                    }
                }
                catch (Exception exception)
                {
                    base.ProcessException(exception);
                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }
            }
        }

        private bool CheckInput()
        {
            this.txtCode.Text = this.txtCode.Text.TrimEnd(new char[0]);
            if (this.txtCode.Text.Trim().Length == 0)
            {
                MessageBox.Show(AppMessage.Format(AppMessage.MSG0007, new string[] { AppMessage.MSG9977 }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.txtCode.Focus();
                return false;
            }
            this.txtFullName.Text = this.txtFullName.Text.TrimEnd(new char[0]);
            if (this.txtFullName.Text.Trim().Length == 0)
            {
                MessageBox.Show(AppMessage.Format(AppMessage.MSG0007, new string[] { AppMessage.MSG9978 }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.txtFullName.Focus();
                return false;
            }
            return true;
        }


        private void EntityToDataTable()
        {
            DataRow row = this.DTModule.Rows[0];
            row[BaseModuleTable.FieldParentID] = (this.ucParent.SelectModule.selectedID != null) ? this.ucParent.SelectModule.selectedID.ToString() : string.Empty;
            row[BaseModuleTable.FieldCode] = this.txtCode.Text;
            row[BaseModuleTable.FieldFullName] = this.txtFullName.Text;
            row[BaseModuleTable.FieldWinForm] = this.txtWinForm.Text;
            row[BaseModuleTable.FieldNavigateUrl] = this.txtNavigateUrl.Text;
            row[BaseModuleTable.FieldTarget] = this.txtTarget.Text;
            row[BaseModuleTable.FieldEnabled] = this.chkEnabled.Checked ? 1 : 0;
            row[BaseModuleTable.FieldIsPublic] = this.chkIsPublic.Checked ? 1 : 0;
            row[BaseModuleTable.FieldIsGroup] = this.chkIsGroup.Checked ? 1 : 0;
            row[BaseModuleTable.FieldDescription] = this.txtDescription.Text;
            this.DTModule.AcceptChanges();
        }

        private void FormOnLoad()
        {
            base.Localization(this);
            this.GetPermission();
            this.DTModule = ServiceManager.Instance.ModuleService.Get(base.UserInfo, this.EntityID);
            this.ucParent.CheckMove = true;
            this.ucParent.SelectModule.openID = this.EntityID;
            this.ShowEntity();
            base.ActiveControl = this.txtCode;
            this.txtCode.Focus();
         
        }


        private void GetPermission()
        {
            bool isAdministrator = base.UserInfo.IsAdministrator;
        }


        private void Pothook()
        {
            string moduleFullName = BaseBusinessLogic.GetProperty(this.DTModule, this.EntityID, BaseModuleTable.FieldFullName);
            if (this.EntityID.Length > 0)
            {
                new ModulePermissionAdminFrm(this.EntityID, moduleFullName).ShowDialog(this);
            }
        }

        private bool SaveEntity()
        {
            bool flag = false;
            this.EntityToDataTable();
            string statusCode = string.Empty;
            string statusMessage = string.Empty;
            ServiceManager.Instance.ModuleService.Update(base.UserInfo, this.DTModule, out statusCode, out statusMessage);
            if (statusCode == StatusCode.OKUpdate.ToString())
            {
                if (BaseSystemInfo.ShowInformation)
                {
                    MessageBox.Show(statusMessage, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                base.DialogResult = System.Windows.Forms.DialogResult.OK;
                flag = true;
            }
            else
            {
                MessageBox.Show(statusMessage, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                if (statusCode == StatusCode.ErrorCodeExist.ToString())
                {
                    this.txtCode.SelectAll();
                    this.txtCode.Focus();
                }
            }
            this.Cursor = Cursors.Default;
            return flag;
        }

        private void ShowEntity()
        {
            BaseModuleEntity entity = new BaseModuleEntity();
            BaseModuleEntity entityParent = new BaseModuleEntity();
            entity.GetFrom(this.DTModule);
            if (string.IsNullOrEmpty(this.ucParent.SelectModule.selectedID))
            {
              
                var selectModule = new UCModuleSelect.SelectModuleInfo();
                DTParent = ServiceManager.Instance.ModuleService.Get(base.UserInfo, entity.ParentID);
                entityParent.GetFrom(DTParent);
              
                selectModule.form = new ModuleSelectFrm();
                selectModule.selectedID = entityParent.ID;
                selectModule.selectedFullName = entityParent.FullName;
                selectModule.openID = entity.ID;
                this.ucParent.SelectModule = selectModule;
            }
            this.txtCode.Text = entity.Code;
            this.txtFullName.Text = entity.FullName;
            this.txtWinForm.Text = entity.WinForm;
            this.txtNavigateUrl.Text = entity.NavigateUrl;
            this.txtTarget.Text = entity.Target;
            this.chkEnabled.Checked = entity.Enabled;
            this.chkIsPublic.Checked = entity.IsPublic;
            this.chkIsGroup.Checked = entity.IsGroup;
            this.txtDescription.Text = entity.Description;
            if (entity.ID.Length == 0)
            {
                MessageBox.Show(AppMessage.MSG0005, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                base.DialogResult = System.Windows.Forms.DialogResult.OK;
                base.Close();
            }
        }

        private void ModuleEditFrm_Load(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                this.FormOnLoad();
            }
            catch (Exception exception)
            {
                base.ProcessException(exception);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void ModuleEditFrm_KeyDown(object sender, KeyEventArgs e)
        {
            string str2;
            if ((((str2 = e.KeyCode.ToString()) != null) && ((str2 == "Return") || (str2 == "Enter"))) && (base.ActiveControl is TextBox))
            {
                SendKeys.Send("{TAB}");
            }
            int keyValue = e.KeyValue;
            if ((keyValue != 0x2e) && (keyValue == 0x74))
            {
                this.FormOnLoad();
            }
        }


    }
}